package com.umfb.mysql.service.diary.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;

import com.umfb.commons.EchartModel;
import com.umfb.demo.diary.mapper.DiaryMapper;
import com.umfb.demo.service.IDiaryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zcah on 18-6-10.
 */
@Service
public class DiaryServiceImpl implements IDiaryService {

    @Autowired
    private DiaryMapper diaryMapper;

    @Override
    public Map<String, List<Object>> showEchartDemo() {
        List<Map<String, Object>> maps = diaryMapper.showEchartDemo();
        List<Object> y = new ArrayList<>();
        List<Object> x = new ArrayList<>();
        List<EchartModel> echartModels = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            EchartModel echartModel = new EchartModel();
            x.add(map.get("typeId").toString());
            y.add(map.get("countNum").toString());
            echartModel.setValue(map.get("countNum").toString());
            echartModel.setName(map.get("typeId").toString());
            echartModels.add(echartModel);
        }
        String json= JSON.toJSONString(echartModels, true);
        System.out.println(json);
        com.alibaba.fastjson.JSONArray objects = com.alibaba.fastjson.JSONArray.parseArray(json);

        System.out.println(maps.size());
        Map<String, List<Object>> returnMap = new HashMap<>();
        returnMap.put("x", x);
        returnMap.put("y", y);
        returnMap.put("echart",objects);
        return returnMap;
    }
}
